; example2.nsi
;
; This script is based on example1.nsi, but it remember the directory, 
; has uninstall support and (optionally) installs start menu shortcuts.
;
; It will install example2.nsi into a directory that the user selects,

;--------------------------------

; Include Modern UI
  !include "MUI2.nsh"

;--------------------------------

; The name of the installer
Name "Hybrid Application Generator"

; Installer branding
; BrandingText "Nokia Hybrid Application Generator"

; Better compression
SetCompressor /SOLID lzma

SetOverwrite ifnewer
CRCCheck on

; The file to write
OutFile "Nokia HAG Installer.exe"

; The default installation directory
InstallDir "$PROGRAMFILES\Nokia\Hybrid Application Generator"

; Registry key to check for directory (so if you install again, it will 
; overwrite the old one automatically)
InstallDirRegKey HKLM "Software\Nokia\Hybrid Application Generator\Installer" "Install_Dir"

; Request application privileges for Windows Vista
RequestExecutionLevel user

;--------------------------------

; Interface Settings
  !define MUI_ABORTWARNING

;--------------------------------

; Pages

  !insertmacro MUI_PAGE_LICENSE "release_current\License.txt"
  !insertmacro MUI_PAGE_COMPONENTS
  !insertmacro MUI_PAGE_DIRECTORY
  !insertmacro MUI_PAGE_INSTFILES

    # These indented statements modify settings for MUI_PAGE_FINISH
    !define MUI_FINISHPAGE_NOAUTOCLOSE
    !define MUI_FINISHPAGE_RUN
    !define MUI_FINISHPAGE_RUN_NOTCHECKED
    !define MUI_FINISHPAGE_RUN_TEXT "Run Hybrid Application Generator"
    !define MUI_FINISHPAGE_RUN_FUNCTION "LaunchApplication"
    ; !define MUI_FINISHPAGE_SHOWREADME_NOTCHECKED
    !define MUI_FINISHPAGE_SHOWREADME $INSTDIR\Readme.txt
  !insertmacro MUI_PAGE_FINISH

  !insertmacro MUI_UNPAGE_CONFIRM
  !insertmacro MUI_UNPAGE_COMPONENTS
  !insertmacro MUI_UNPAGE_INSTFILES
  !insertmacro MUI_UNPAGE_FINISH

;--------------------------------

;Languages
  !insertmacro MUI_LANGUAGE "English"

;--------------------------------

; The stuff to install
Section "Core Application" Core

  SectionIn RO
  
  ; Set output path to the installation directory.
  SetOutPath $INSTDIR
  
  ; Put the files
  File /r /x examples release_current\*.*
  
  ; Write the installation path into the registry
  WriteRegStr HKLM "Software\Nokia\Hybrid Application Generator" "Install_Dir" "$INSTDIR"
  
  ; Write the uninstall keys for Windows
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Hybrid Application Generator" "DisplayName" "Nokia Hybrid Application Generator"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Hybrid Application Generator" "UninstallString" '"$INSTDIR\uninstall.exe"'
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Hybrid Application Generator" "NoModify" 1
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Hybrid Application Generator" "NoRepair" 1
  WriteUninstaller "uninstall.exe"
  
SectionEnd

; Examples -- optional
Section "Demo Applications" Demos

  SetOutPath $INSTDIR\examples
  File /r release_current\examples\*.*
  SetOutPath $INSTDIR

SectionEnd

; Optional section (can be disabled by the user)
Section "Start Menu Shortcuts" Shortcuts

  CreateDirectory "$SMPROGRAMS\Nokia Hybrid Application Generator"
  CreateShortCut "$SMPROGRAMS\Nokia Hybrid Application Generator\Hybrid Application Generator.lnk" "$INSTDIR\HAG.exe" "" "$INSTDIR\HAG.exe" 0
  CreateShortCut "$SMPROGRAMS\Nokia Hybrid Application Generator\Documentation.lnk" "$INSTDIR\HAG User Guide.pdf" "" "$INSTDIR\HAG User Guide.pdf" 0
  CreateShortCut "$SMPROGRAMS\Nokia Hybrid Application Generator\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
  
SectionEnd

;--------------------------------

; Section descriptions
LangString DESC_Core ${LANG_ENGLISH} "Installs Hybrid Application Generator main executable, framework files, and the default set of HAG plugins."
LangString DESC_Demos ${LANG_ENGLISH} "Installs demo applications. The demo applications are introduced in the HAG User Guide."
LangString DESC_Shortcuts ${LANG_ENGLISH} "Installs start menu shortcuts for easier access to application."

!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
  !insertmacro MUI_DESCRIPTION_TEXT ${Core} $(DESC_Core)
  !insertmacro MUI_DESCRIPTION_TEXT ${Demos} $(DESC_Demos)
  !insertmacro MUI_DESCRIPTION_TEXT ${Shortcuts} $(DESC_Shortcuts)
!insertmacro MUI_FUNCTION_DESCRIPTION_END

;--------------------------------

; Uninstaller

Section "un.Core Application" un.Core
  
  SectionIn RO

  ; Remove registry keys
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Hybrid Application Generator"
  DeleteRegKey HKLM "Software\Nokia\Hybrid Application Generator"

  ; Remove the application. Do not use simple recursive $INSTDIR removal, since that will actually cause
  ; the removal of the current directory of uninstall.exe, potentially wiping out everything.
  RMDir /r "$INSTDIR\examples"
  RMDir /r "$INSTDIR\fw"
  RMDir /r "$INSTDIR\iconengines"
  RMDir /r "$INSTDIR\oxygen-icons"
  RMDir /r "$INSTDIR\imageformats"
  RMDir /r "$INSTDIR\mw"
  RMDir /r "$INSTDIR\templates"

  ; HAG executables, guide, and default log file
  Delete /REBOOTOK "$INSTDIR\HAG User Guide.pdf"
  Delete /REBOOTOK "$INSTDIR\HAG.exe"
  Delete /REBOOTOK "$INSTDIR\launcher.exe"
  Delete /REBOOTOK "$INSTDIR\unzip.exe"
  Delete /REBOOTOK "$INSTDIR\wget.exe"
  Delete /REBOOTOK "$INSTDIR\uninstall.exe"
  Delete /REBOOTOK "$INSTDIR\hag.log"

  ; HAG runtimes
  Delete /REBOOTOK "$INSTDIR\msvcr90.dll"
  Delete /REBOOTOK "$INSTDIR\Qt*.dll"
  Delete /REBOOTOK "$INSTDIR\phonon4.dll"
  Delete /REBOOTOK "$INSTDIR\hybridfw.dll"
  Delete /REBOOTOK "$INSTDIR\locationapi.dll"

  ; wget runtimes
  Delete /REBOOTOK "$INSTDIR\libeay32.dll"
  Delete /REBOOTOK "$INSTDIR\libiconv2.dll"
  Delete /REBOOTOK "$INSTDIR\libintl3.dll"
  Delete /REBOOTOK "$INSTDIR\libssl32.dll"

  ; Licenses, readme
  Delete /REBOOTOK "$INSTDIR\License.txt";
  Delete /REBOOTOK "$INSTDIR\Readme.txt"

  ; Now, this will only work if the directory is empty
  RMDir "$INSTDIR"

  ; Remove shortcuts, if any
  Delete "$SMPROGRAMS\Nokia Hybrid Application Generator\*.*"

  ; Remove directories used
  RMDir "$SMPROGRAMS\Nokia Hybrid Application Generator"
  RMDir "$INSTDIR"

SectionEnd

Section /o "un.Preferences" un.Preferences

  ; Remove permanent settings from the current user
  DeleteRegKey HKCU "Software\Nokia\Hybrid Application Generator"

SectionEnd

;--------------------------------

; Section descriptions
LangString DESC_un.Core ${LANG_ENGLISH} "Removes Hybrid Application Generator main executable, framework files, plugins, demo applications, and start menu shortcuts."
LangString DESC_un.Preferences ${LANG_ENGLISH} "Removes Hybrid Application Generator permanent settings."

!insertmacro MUI_UNFUNCTION_DESCRIPTION_BEGIN
  !insertmacro MUI_DESCRIPTION_TEXT ${un.Core} $(DESC_un.Core)
  !insertmacro MUI_DESCRIPTION_TEXT ${un.Preferences} $(DESC_un.Preferences)
!insertmacro MUI_UNFUNCTION_DESCRIPTION_END

;--------------------------------

; Launch application?

Function LaunchApplication
  ExecShell "" "$INSTDIR\HAG.exe"
FunctionEnd
